package de.baumann.browser.present

import de.baumann.browser.api.OdinObserver
import de.baumann.browser.api.net.vo.NewsItem
import de.baumann.browser.base.BasePresenter
import de.baumann.browser.iview.INewsView
import de.baumann.browser.model.NewsModel
import de.baumann.browser.utils.Constants

class NewsListPresenter : BasePresenter<INewsView>() {
    private var isRefresh = true
    private var startPage = 0
    private val newsModel = NewsModel()

    fun refresh() {
        isRefresh = true
        startPage = 0
        loadNews(Constants.LOAD_REFRESH)
    }

    fun loadMore() {
        isRefresh = false
        startPage += 1
        loadNews(Constants.LOAD_MORE)
    }

    fun loadNormal() {
        isRefresh = true
        startPage = 0
        loadNews(Constants.LOAD_NORMAL)
    }

    private fun loadNews(loadType: Int) {
        this.newsModel.getNews(view.columnId, startPage, loadType)
                .subscribe(object : OdinObserver<List<NewsItem>>() {
                    override fun onSuccess(data: List<NewsItem>?) {
                        if (view != null && data != null) {
                            view.refreshNewsList(data, isRefresh)
                        }
                    }
                })
    }

}